﻿package mortal.game.resource.tableConfig
{
    import Message.DB.Tables.*;
    import __AS3__.vec.*;
    import com.gengine.resource.*;
    import flash.utils.*;
    import mortal.game.view.CollectExchange.other.*;
    import mortal.game.view.common.*;

    public class CollectExchangeConfig extends Object
    {
        private var _exchangeDic:Dictionary;
        private var _maxCount:int = 0;
        private var _exchangeGroups:Vector.<CollectExchangeDataGroup>;
        private static var _instance:CollectExchangeConfig;

        public function CollectExchangeConfig()
        {
            if (_instance != null)
            {
                throw new Error(" CollectRewardConfig 单例 ");
            }
            this.init();
            return;
        }// end function

        public function init() : void
        {
            var _loc_1:* = ConfigManager.instance.getJSONByFileName("t_collect_config.json");
            this.write(_loc_1);
            return;
        }// end function

        private function write(param1:Object) : void
        {
            var _loc_2:TCollectConfig = null;
            var _loc_3:Object = null;
            if (!this._exchangeDic)
            {
                this._exchangeDic = new Dictionary();
            }
            for each (_loc_3 in param1)
            {
                
                _loc_2 = new TCollectConfig();
                ClassTypesUtil.copyValue(_loc_2, _loc_3);
                this._exchangeDic[_loc_2.id] = _loc_2;
                var _loc_6:String = this;
                var _loc_7:* = this._maxCount + 1;
                _loc_6._maxCount = _loc_7;
            }
            return;
        }// end function

        public function get ExchangeGroup() : Vector.<CollectExchangeDataGroup>
        {
            var id:*;
            var sortFunc:Function;
            var exchangeInfo:TCollectConfig;
            var exchangeStr:String;
            var dataItemVector:Vector.<CollectExchangeDataItem>;
            var needNum:int;
            var maxTimes:int;
            var group:* = new Vector.<CollectExchangeDataGroup>;
            var _loc_2:int = 0;
            var _loc_3:* = this._exchangeDic;
            while (_loc_3 in _loc_2)
            {
                
                id = _loc_3[_loc_2];
                exchangeInfo = this._exchangeDic[id];
                exchangeStr = exchangeInfo.recipe + "&" + exchangeInfo.output;
                dataItemVector = this.getExchangeDataItemVectorByExchangeString(exchangeStr);
                needNum = this.getNeedNumByInputString(exchangeInfo.recipe);
                maxTimes = exchangeInfo.exchangeNum;
                if (id > 0)
                {
                    group.push(this.createExchange(dataItemVector, needNum, maxTimes, id));
                }
            }
            sortFunc = function (param1:CollectExchangeDataGroup, param2:CollectExchangeDataGroup) : Number
            {
                if (param1 == null || param1 == null)
                {
                    return 1;
                }
                if (param1.id > param2.id)
                {
                    return 1;
                }
                if (param1.id < param2.id)
                {
                    return -1;
                }
                return 0;
            }// end function
            ;
            group.sort(sortFunc);
            return group;
        }// end function

        private function getExchangeDataItemVectorByExchangeString(param1:String) : Vector.<CollectExchangeDataItem>
        {
            var _loc_5:Array = null;
            var _loc_6:CollectExchangeDataItem = null;
            var _loc_2:* = new Vector.<CollectExchangeDataItem>;
            var _loc_3:* = param1.split("&");
            var _loc_4:int = 0;
            while (_loc_4 < _loc_3.length)
            {
                
                _loc_5 = (_loc_3[_loc_4] as String).split("#");
                _loc_6 = new CollectExchangeDataItem(int(_loc_5[0]), int(_loc_5[1]));
                _loc_2[_loc_4] = _loc_6;
                _loc_4++;
            }
            return _loc_2;
        }// end function

        private function createExchange(param1:Vector.<CollectExchangeDataItem>, param2:int, param3:int, param4:int) : CollectExchangeDataGroup
        {
            var _loc_5:* = new CollectExchangeDataGroup();
            _loc_5.needNum = param2;
            _loc_5.dataItemList = param1;
            var _loc_6:* = param3;
            _loc_5.maxTimes = param3;
            _loc_5.leftTimes = _loc_6;
            _loc_5.id = param4;
            return _loc_5;
        }// end function

        private function getNeedNumByInputString(param1:String) : int
        {
            var _loc_2:* = param1.split("&");
            return _loc_2.length;
        }// end function

        public static function get instance() : CollectExchangeConfig
        {
            if (_instance == null)
            {
                _instance = new CollectExchangeConfig;
            }
            return _instance;
        }// end function

    }
}
